﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace FYPDemo
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            int ID = Convert.ToInt32(e.CommandArgument);
            int quantity = Convert.ToInt32(ViewState["Quantity"]);
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDDLCategory();
                string category = Request.QueryString["category"];
                string vendor = Request.QueryString["vendor"];

                if (category != "")
                {
                    DataTable dt = ProductManagement.displayProduct(category, "");

                    ListView1.DataSource = dt;
                    ListView1.DataBind();
                }

                if (vendor != "")
                {
                    ViewState["vendor"] = vendor;
                    DataTable dt = ProductManagement.displayProduct("All", vendor);

                    ListView1.DataSource = dt;
                    ListView1.DataBind();
                }
            }
        }
        protected void btnAddCart(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            DropDownList ddl = (DropDownList)b.Parent.FindControl("ddlQuantity");
            Label lbPrice = (Label)b.Parent.FindControl("lblUnitPrice");
            HiddenField hf = (HiddenField)b.FindControl("hfID");
            int id = Convert.ToInt32(hf.Value);
            int quantity = Convert.ToInt32(ddl.SelectedValue);
            ViewState["Quantity"] = quantity;

            string name = Session["username"].ToString();
            string productID = ProductManagement.getProductIDByID(id);
            ViewState["productID"] = productID;
            double prodPrice = Convert.ToDouble(lbPrice.Text);
            string orderID = "";
            string prefix = "";
            string runNo = "";
            int runningNo = 0;
            
            int prodRedundant = 0;

            prodPrice = prodPrice * quantity;

            ProductManagement.cartManagement();

            DataTable dt = ProductManagement.getLatestCart(name);

            if (dt.Rows.Count > 0)
            {
                orderID = dt.Rows[0][0].ToString();
            }

            int currentStock = 0;
            int safeLevel = 0;
            DataTable dt1 = ProductManagement.checkStockLevel(ViewState["productID"].ToString());

            if (dt1.Rows.Count > 0)
            {
                currentStock = Convert.ToInt32(dt1.Rows[0][0]);
                safeLevel = Convert.ToInt32(dt1.Rows[0][1]);
            }

            if ((currentStock - quantity) > 0)
            {

                if ((currentStock - quantity) <= safeLevel)
                {
                    //send mail to inform vendor below safe level
                    MessageBox.Show("below safe level");
                }

                if (orderID != "")
                {
                    prodRedundant = ProductManagement.checkRedundantProduct(orderID, productID);

                    if (prodRedundant > 0)
                    {

                        ProductManagement.combineRedundantProduct(orderID, productID, quantity, prodPrice);
                        ProductManagement.deductStock(productID, quantity);
                    }

                    else
                    {
                        ProductManagement.insertOrderDetails(orderID, productID, quantity, prodPrice);
                        ProductManagement.deductStock(productID, quantity);
                    }
                }

                else
                {
                    orderID = ProductManagement.getLatestOrderID();

                    if (orderID != "")
                    {
                        string num = orderID.Remove(0, 1);
                        prefix = orderID.Substring(0, 1);
                        runningNo = Convert.ToInt32(num) + 1;
                    }

                    else
                    {
                        runningNo = 1;
                        prefix = "U";
                    }

                    runNo = String.Format("{0:00000}", runningNo);
                    orderID = prefix + runNo;
                                        
                    ProductManagement.insertOrder(orderID, name);
                    ProductManagement.insertOrderDetails(orderID, productID, quantity, prodPrice);
                    ProductManagement.deductStock(productID, quantity);
                }
            }

            else
            {
                MessageBox.Show("Not enough stock");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string vendor = "";
            string category = ddlCategory.SelectedItem.ToString();

            if(ViewState["vendor"] != null)
                vendor = ViewState["vendor"].ToString();

            DataTable dt = ProductManagement.displayProduct(category, vendor);

            ListView1.DataSource = dt;
            ListView1.DataBind();
        }

        protected void bindDDLCategory()
        {
            DataTable dt = ProductManagement.bindProductCategory();

            ddlCategory.DataSource = dt;
            ddlCategory.DataTextField = "Value";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Shopping.aspx");
        }
    }
}